#include <iostream>
#include <algorithm>
#include <string>
#include <set>

using namespace std;

long long toint(char c)
{
    return c - '0';
}

long long arr2[32] = {0};
long long get2(int n)
{
    int i;
    if (n == 0)
    {
        return 1;
    }
    if (arr2[n])
    {
        return arr2[n];
    }

    arr2[n] = 2;
    for (i = 0; i < n - 1; i++)
    {
        arr2[n] *= 2;
    }
    return arr2[n];
}

long long arr3[32] = {0};
long long get3(int n)
{
    int i;
    if (n == 0)
    {
        return 1;
    }
    if (arr3[n])
    {
        return arr3[n];
    }

    arr3[n] = 3;
    for (i = 0; i < n - 1; i++)
    {
        arr3[n] *= 3;
    }
    return arr3[n];
}

long long from2(string s)
{
    int i, ret = 0;
    for (i = 0; i < s.size(); i++)
    {
        if (toint(s[s.size() - 1 - i]))
        {
            ret += get2(i);
        }
    }
    return ret;
}

long long from3(string s)
{
    int i, ret = 0;
    for (i = 0; i < s.size(); i++)
    {
        ret += get3(i) * toint(s[s.size() - 1 - i]);
    }
    return ret;
}

int main(int argc, char const *argv[])
{
    set<int> temp2;
    string n2, n3, t2, t3;
    int i, j, ni, nj;
    long long v2, v3;
    cin >> n2 >> n3;

    for (ni = 0; ni < n2.size(); ni++)
    {
        t2 = n2;
        for (i = 0; i <= 1; i++)
        {
            t2[ni] = i + '0';
            v2 = from2(t2);
            temp2.insert(v2);
        }
    }

    for (nj = 0; nj < n3.size(); nj++)
    {
        t3 = n3;
        for (j = 0; j <= 3; j++)
        {
            t3[nj] = j + '0';
            v3 = from3(t3);

            if (temp2.find(v3) != temp2.end())
            {
                cout << v3;
                return 0;
            }
        }
    }

    return 0;
}
